package com.znsd.stu.controllor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.znsd.entitys.ClassInfo;
import com.znsd.entitys.NoticeInfo;
import com.znsd.entitys.Users;
import com.znsd.stu.service.ClazzService;
import com.znsd.stu.service.NoticeService;
/**
 * 
 * @ClassName: NoticeControllor
 * @Description: TODO(公告)
 * @author Liuyl
 * @date 2019年8月23日
 *
 */
@Controller
public class NoticeControllor {
	
	@Autowired
	private NoticeService noticeService;
	
	@Autowired
	private ClazzService clazzService;
	
	@RequestMapping("noticelist")
	public String noticelist() {
		return "admin/noticelist";
	}
	@ResponseBody
	@RequestMapping("getNoticelist")
	public Map<String,Object> getNoticelist(Integer page,Integer limit) {
		Integer pageIndex=(page-1)*limit;
		List<NoticeInfo> queryNoticeAll = noticeService.queryNoticeAll(pageIndex,limit);
		for (NoticeInfo noticeInfo : queryNoticeAll) {
			System.out.println("公告++++"+noticeInfo);
		}
		Integer queryNoticeCount = noticeService.queryNoticeCount();
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "成功");
		map.put("count", queryNoticeCount);
		map.put("data", queryNoticeAll);
		return map;
	}
	
	@RequestMapping(value = "noticeadd",method = RequestMethod.GET)
	public String getNoticelist() {
		return "admin/noticeadd";
	}
	
	@ResponseBody
	@RequestMapping("getClazzAll")
	public Map<String,Object> getClazzAll() {
		List<ClassInfo> clazzAll = clazzService.queryClazzAll();
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "成功");
		map.put("data", clazzAll);
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "noticeadd",method = RequestMethod.POST)
	public Map<String,Object> noticeAdd(NoticeInfo notice,Integer clazz,HttpServletRequest request) {
		System.out.println("------------------");
		System.out.println(notice+"-------"+clazz);
		HttpSession session = request.getSession();
		Users user=(Users) session.getAttribute("user");
		Integer notice_id=noticeService.addNotice(user,notice,clazz);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("data", "成功");
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "noticeupdate",method = RequestMethod.POST)
	public Map<String,Object> noticeUpdate(NoticeInfo notice,Integer clazz) {
		Integer notice_id=noticeService.updateNotice(notice,clazz);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("data", "成功");
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value = "noticedel",method = RequestMethod.POST)
	public Map<String,Object> noticeDel(HttpServletRequest request) {
		String id = request.getParameter("id");
		String[] split = id.split(",");
		for (String string : split) {
			Integer del=noticeService.delNotice(Integer.parseInt(string));
		}
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("data", "成功");
		return map;
	}
}
